/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package admin

import (
	"gitee.com/tugoer/orivil/bundles/db"
	"gitee.com/tugoer/orivil/config"
)

const SuperAdminLevel = 30

var _ = func() struct{} {
	db.Gorm.AutoMigrate(
		&Admin{},
		//&Nav{},
	)

	// 添加默认管理员
	var adminNum int
	err := db.Gorm.Model(&Admin{}).Count(&adminNum).Error
	if err != nil {
		panic(err)
	}
	if adminNum == 0 {
		// 默认管理员
		var ads = []*Admin{{
			AllowedSign: 1,
			Name: "admin",
			Password: "123456",
			Level: SuperAdminLevel,
		}}
		parser := config.NewJsonParser("configs")
		err = parser.ReadOrCreate("admins", &ads)
		if err != nil {
			panic(err)
		}
		if len(ads) == 0 || ads[0].Name == "" {
			panic("未设置超级管理员")
		} else {
			for _, a := range ads {
				err = a.Create()
				if err != nil {
					panic(err)
				}
			}
		}
	}
	return struct{}{}
}()